/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.evidence.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.z100h.supplychain.core.EvidenceType;
import com.z100h.supplychain.evidence.entity.TransferEvidence;
import com.z100h.supplychain.evidence.facade.TransferFacade;
import com.z100h.supplychain.evidence.service.TransferEvidenceService;
import com.z100h.supplychain.sku.entity.SkuAmount;
import com.z100h.supplychain.sku.entity.SkuAmountPo;

/**
 * 转库凭证Controller
 * @author fyh
 * @version 2019-01-04
 */
@Controller
@RequestMapping(value = "${adminPath}/transferevidence/transferEvidence")
public class TransferEvidenceController extends BaseController {

	@Autowired
	private TransferFacade<TransferEvidence> transferFacade;

	@Autowired
	private TransferEvidenceService transferEvidenceService;

	@ModelAttribute
	public TransferEvidence get(@RequestParam(required = false) String id) {
		TransferEvidence entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = transferEvidenceService.get(id);
		}
		if (entity == null) {
			entity = new TransferEvidence();
		}
		return entity;
	}

	/**
	 * 转库凭证列表页面
	 */
	@RequiresPermissions("transferevidence:transferEvidence:list")
	@RequestMapping(value = { "list", "" })
	public String list() {
		return "supplychain/evidence/transferEvidenceList";
	}

	@RequestMapping(value = "resourceList")
	public String resourceList() {
		return "supplychain/evidence/resourceList";
	}

	/**
	 * 转库凭证列表数据
	 */
	@ResponseBody
	@RequiresPermissions("transferevidence:transferEvidence:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(TransferEvidence transferEvidence, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<TransferEvidence> page = transferEvidenceService.findPage(new Page<TransferEvidence>(request, response),
				transferEvidence);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑转库凭证表单页面
	 */
	@RequiresPermissions(value = { "transferevidence:transferEvidence:view", "transferevidence:transferEvidence:add",
			"transferevidence:transferEvidence:edit" }, logical = Logical.OR)
	@RequestMapping(value = "form")
	public String form(TransferEvidence transferEvidence, Model model) {
		model.addAttribute("transferEvidence", transferEvidence);
		if (StringUtils.isBlank(transferEvidence.getId())) {// 如果ID是空为添加
			model.addAttribute("isAdd", true);
			return "supplychain/evidence/transferEvidenceForm";
		}
		return "supplychain/evidence/transferCheckForm";
	}

	/**
	 * 保存转库凭证
	 * 
	 * TODO  转库需要另开发
	 */
	@RequiresPermissions(value = { "transferevidence:transferEvidence:add",
			"transferevidence:transferEvidence:edit" }, logical = Logical.OR)
	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, String> save(TransferEvidence transferEvidence, Model model,
			RedirectAttributes redirectAttributes) throws Exception {
		// if (!beanValidator(model, transferEvidence)) {
		// return form(transferEvidence, model);
		// }
		// 新增或编辑表单保存
		Map<String, String> map = new HashMap<String, String>();
		try {
			transferFacade.transferProcess(transferEvidence.getResourceIds(), transferEvidence, EvidenceType.TRANSFER);
			map.put("code", "0");
			map.put("message", "保存转库单成功");
			// addMessage(redirectAttributes, "保存转库单成功");
		} catch (Throwable e) {
			e.printStackTrace();
			map.put("code", "1");
			map.put("message", e.getMessage());
		}
		// return "redirect:" + Global.getAdminPath() + "/transferevidence/transferEvidence/?repage";
		return map;
	}

	/**
	 * 
	 * 内部生产消耗转库
	 * 
	 * @param out
	 * @param comment
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年7月7日 下午3:10:20
	 */
	@RequestMapping(value = "mp/insideTransfer")
	@ResponseBody
	public AjaxJson insideTransfer(HttpServletRequest request, @RequestBody SkuAmountPo skuAmountPo) {
		AjaxJson respBody = new AjaxJson();
		try {
			String warehouseType = "9f24355a978a409e9379c6179cf3bd97";
			if (localLogin(request)) {
				transferFacade.save(EvidenceType.TRANSFER, warehouseType, skuAmountPo.getOut(),
						skuAmountPo.getComment());
			}
		} catch (Throwable e) {
			e.printStackTrace();
			respBody.setSuccess(false);
			respBody.setMsg("内部消耗转库异常：" + e.getMessage());
		}
		return respBody;
	}

	/**
	 * 
	 * 报废转库
	 * 
	 * 暂时没被调用
	 * 
	 * @param out
	 * @param comment
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年7月7日 下午3:10:20
	 */
	@RequestMapping(value = "scrapTransfer")
	@ResponseBody
	public Map<String, String> scrapTransfer(List<SkuAmount> out, String comment) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			String warehouseType = "4";
			transferFacade.save(EvidenceType.TRANSFER, warehouseType, out, comment);
			map.put("code", "0");
			map.put("message", "保存转库单成功");
		} catch (Throwable e) {
			e.printStackTrace();
			map.put("code", "1");
			map.put("message", e.getMessage());
		}
		return map;
	}
}